<template>
  <div>
    <el-dialog :title="types=='add'?'新增':'修改'" :visible="isShow" :before-close="close" width="60%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" @submit.native.prevent>
        <el-form-item label="商品名称" prop="goodsname">
          <el-input v-model="ruleForm.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="一级分类">
          <el-select v-model="ruleForm.first_cateid" placeholder="请选择一级分类" @change="firstChange">
            <el-option :label="role.catename" :value="role.id" v-for="(role,index) in firstList" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="ruleForm.second_cateid" placeholder="请选择二级分类" @change="firstChange">
            <el-option :label="role.catename" :value="role.id" v-for="(role,index) in secondList" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格" prop="market_price">
          <el-input v-model="ruleForm.market_price"></el-input>
        </el-form-item>
        <!-- action 上传的地址 -->
        <!-- limit 上传的数量限制 -->
        <!-- on-exceed 超出限制时执行的函数 -->
        <!-- auto-upload 自动上传 这里的接口是所有数据一起上传 所以关闭自动上传 -->
        <!-- on-change 文件变化时的钩子 -->
        <!-- file-list 历史文件的列表 -->
        <el-form-item label="图片">
          <el-upload action="#" list-type="picture-card" :limit="3" :on-exceed="handleExceed" :auto-upload="false" :on-change="handleChange" :file-list="fileList">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品规格属性" prop="specsattr">
          <div v-for="(item,index) in specsattrList" :key="index">
            <el-input v-model="specsattrList[index]" class="attrs"></el-input>
            <el-button type="danger" @click="delSpecs(index)">删除</el-button>
          </div>
          <el-button @click="addDomain" type="success">添加</el-button>
        </el-form-item>
        <el-form-item label="是否新品" prop="isnew">
          <el-radio-group v-model="ruleForm.isnew">
            <el-radio :label="1">新品</el-radio>
            <el-radio :label="2">不是新品</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否热卖" prop="ishot">
          <el-radio-group v-model="ruleForm.ishot">
            <el-radio :label="1">热卖</el-radio>
            <el-radio :label="2">不是热卖</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click.stop="close">取消</el-button>
        <el-button @click.stop="submit" type="primary">
          确定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateListTree, getCateListByTop, addGoods, editGoods } from '@/api/index'
export default {
  name: 'MyWin',
  props: ['isShow', 'winList', 'types'],
  data () {
    return {
      // 属性值的数据，会根据数组的长度 生成不同的输入框
      specsattrList: [''],
      // 上传过的历史文件
      fileList: [],
      // 下拉的数据 一级下拉菜单，二级下拉菜单
      firstList: [],
      secondList: [],
      ruleForm: {
        goodsname: '',
        price: '',
        specsattr: [''],
        isnew: '1',
        ishot: '1',
        status: '1',
        market_price: '',
        specsid: '3'
      },
      rules: {
        goodsname: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      }
    }
  },
  methods: {
    close () {
      this.$emit('updates')
    },
    submit () {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          this.ruleForm.specsattr = this.specsattrList.join(',')
          delete this.ruleForm.firstcatename
          delete this.ruleForm.secondcatename
          // 只要请求中可能包含图片 就需要将数据类型转为formdata 提交给后台的就是这个类型的数据
          const fd1 = new FormData()
          // 现在的fd1是空的 数据来源是ruleForm 需要将ruleForm中的数据存入fd1 并提交
          // 遍历ruleForm 将数据存入fd1
          for (const key in this.ruleForm) {
            fd1.append(key, this.ruleForm[key])
          }
          let res
          // eslint-disable-next-line
          if (this.types == 'add') {
            res = await addGoods(fd1)
            // eslint-disable-next-line
            if (res.code == 200) {
              this.$emit('update')
            }
          } else {
            res = await editGoods(fd1)
            // eslint-disable-next-line
            if (res.code == 200) {
              this.$emit('update')
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
      // 调用关闭函数
      this.close()
    },
    setVal (data) {
      // 给表单项赋值
      console.log(data)
      this.ruleForm = { ...data }
      // eslint-disable-next-line
      data.goodsname == '' ? (this.specsattrList = this.ruleForm.specsattr) : (this.specsattrList = this.ruleForm.specsattr.split(','))
    },
    async getFirstList () {
      const res = await getCateListTree()
      this.firstList = res.list
    },
    async firstChange () {
      const res = await getCateListByTop({ pid: this.ruleForm.first_cateid })
      this.secondList = res.list
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    handleChange (file) {
      // 这里获取了文件
      this.ruleForm.img = file.raw
    },
    addDomain () {
      this.specsattrList.push('')
    },
    delSpecs (index) {
      // 删除当前属性值
      this.specsattrList.splice(index, 1)
    }
  },
  created () {
    // 发请求 获取下拉菜单的数据
    this.getFirstList()
  }
}
</script>

<style scoped>
.attrs {
  width: 80%;
  margin-right: 20px;
  margin-left: 0;
  margin-bottom: 10px;
}
</style>
